home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
pascal
/
ooptut34.zip
/
TP
/
OOPTUTOR
/
STREAMS.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1993-06-14
|
12KB
|
390 lines
{$A+,B-,D+,E+,F-,G-,I+,L+,N-,O-,R-,S+,V+,X-}
{$M 4096,0,20000}
{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
{ Turbo Pascal 6.0 Demo program from the Turbo Vision Guide. }
{ }
{ TVGUID21.PAS Copyright (c) 1990 by Borland International }
{ }
{ Modification 10.8.91. Further addition 21.11.92. }
{ Now provides screen display of details of Stream Registration and }
{ Graphical Shapes. DOS Debug is also used to display a memory check }
{ for the collection of graphical shapes and to inspect the stream }
{ file GRAPHICS.STM }
{ }
{ STREAMS.PAS -> .EXE R Shaw 21.11.92 }
{ }
{_______________________________________________________________________}
{ Create and display a collection of graphical objects:
Points, Circles, Rectangles. Then put them on a stream
to be read by another program (TVGUID22.PAS).
If you are running this program in the IDE, be sure to
enable the full graphics save option when you load TURBO.EXE:
turbo -g
This ensures that the IDE fully swaps video RAM and keeps
"dustclouds" from appearing on the user screen when in
graphics mode. You can enable this option permanently
via the Options|Environment|Startup dialog.
This program uses the Graph unit and its .BGI driver files to
display graphics on your system. The "PathToDrivers"
constant defined below is now set to \TP\OOPTUTOR, instead of
\TP\BGI as in the original Borland program.
}
program STREAMS;
uses
Dos, Objects, Graph, Crt, Hex;
const
PathToDrivers = '\TP\OOPTUTOR';
var
answer : char;
reply : char;
MaxX, MaxY : integer;
{ ********************************** }
{ ****** Graphical Objects ******* }
{ ********************************** }
type
PGraphObject = ^TGraphObject;
TGraphObject = object(TObject)
X,Y: Integer;
constructor Init;
procedure Draw; virtual;
procedure Store(var S: TStream); virtual;
end;
PGraphPoint = ^TGraphPoint;
TGraphPoint = object(TGraphObject)
procedure Draw; virtual;
end;
PGraphCircle = ^TGraphCircle;
TGraphCircle = object(TGraphObject)
Radius: Integer;
constructor Init;
procedure Draw; virtual;
procedure Store(var S: TStream); virtual;
end;
PGraphRect = ^TGraphRect;
TGraphRect = object(TGraphObject)
Width, Height: Integer;
constructor Init;
procedure Draw; virtual;
procedure Store(var S: TStream); virtual;
end;
{ TGraphObject }
constructor TGraphObject.Init;
begin
X := Random(MaxX);
Y := Random(MaxY);
write('X = ',X:3,' Y = ',Y:3); {Added by RS to display values for }
end; { checking by DOS Debug graphics.stm }
procedure TGraphObject.Draw;
begin
Abstract; { Give error: This object should never be drawn }
end;
procedure TGraphObject.Store(var S: TStream);
begin
S.Write(X, SizeOf(X));
S.Write(Y, SizeOf(Y));
end;
{ TGraphPoint }
procedure TGraphPoint.Draw;
var
DX, DY: Integer;
begin
{ Make it a fat point so you can see it }
for DX := x - 2 to x + 2 do
for DY := y - 2 to y + 2 do
PutPixel(DX, DY, 1);
end;
{ TGraphCircle }
constructor TGraphCircle.Init;
begin
TGraphObject.Init; { RS addition will display X and Y }
Radius := 20 + Random(20);
Write(' Radius = ',Radius:3); { RS addition to display radius for }
end; { checking by DOS Debug graphics.stm }
procedure TGraphCircle.Draw;
begin
Circle(X, Y, Radius);
end;
procedure TGraphCircle.Store(var S: TStream);
begin
TGraphObject.Store(S);
S.Write(Radius, SizeOf(Radius));
end;
{ TGraphRect }
constructor TGraphRect.Init;
begin
TGraphObject.Init; { RS addition will display X and Y }
Width := 10 + Random(20) + X;
Height := 6 + Random(15) + Y;
write(' Width = ',Width:3,' Height = ',Height:3); { RS addition to }
end; { display width }
{ and height }
procedure TGraphRect.Draw;
begin
Rectangle(X, Y, X + Width, Y + Height);
end;
procedure TGraphRect.Store(var S: TStream);
begin
TGraphObject.Store(S);
S.Write(Width, SizeOf(Width));
S.Write(Height, SizeOf(Height));
end;
{ ********************************** }
{ ** Stream Registration Records ** }
{ ********************************** }
const
RGraphPoint: TStreamRec = (
ObjType: 150;
VmtLink: Ofs(TypeOf(TGraphPoint)^);
Load: nil; { No load method yet }
Store: @TGraphPoint.Store);
RGraphCircle: TStreamRec = (
ObjType: 151;
VmtLink: Ofs(TypeOf(TGraphCircle)^);
Load: nil; { No load method yet }
Store: @TGraphCircle.Store);
RGraphRect: TStreamRec = (
ObjType: 152;
VmtLink: Ofs(TypeOf(TGraphRect)^);
Load: nil; { No load method yet }
Store: @TGraphRect.Store);
{ ********************************** }
{ ************ Globals ************ }
{ ********************************** }
{ Abort the program and give a message }
procedure Abort(Msg: String);
begin
Writeln;
Writeln(Msg);
Writeln('Program aborting');
Halt(1);
end;
{ Register all object types that will be put onto the stream.
This includes standard TVision types, like TCollection.
}
procedure StreamRegistration;
begin
writeln('DETAILS OF STREAM REGISTRATION');
RegisterType(RCollection);
RegisterType(RGraphPoint);
Writeln;
writeln('RGraphPoint.ObjType: ',RGraphPoint.ObjType);
writeln('RGraphPoint.VmtLink: ',RGraphPoint.VmtLink);
writeln;
RegisterType(RGraphCircle);
writeln('RGraphCircle.ObjType: ',RGraphCircle.ObjType);
writeln('RGraphCircle.VmtLink: ',RGraphCircle.VmtLink);
writeln;
RegisterType(RGraphRect);
writeln('RGraphRect.ObjType: ',RGraphRect.ObjType);
writeln('RGraphRect.VmtLink: ',RGraphRect.VmtLink);
writeln;
write('Press any key to continue ');
answer := readkey; { Pause to view registration data }
ClrScr;
end;
{ Put the system into graphics mode }
procedure StartGraphics;
var
Driver, Mode: Integer;
begin
Driver := Detect;
InitGraph(Driver, Mode, PathToDrivers);
if GraphResult <> GrOK then
begin
Writeln(GraphErrorMsg(Driver));
if Driver = grFileNotFound then
begin
Writeln('in ', PathToDrivers,
'. Modify this program''s "PathToDrivers"');
Writeln('constant to specify the actual location of this file.');
Writeln;
end;
Writeln('Press Enter...');
Readln;
Halt(1);
end;
end;
{ Use the ForEach iterator to traverse and
show all the collection of graphical objects.
}
procedure DrawAll(C: PCollection);
{ Nested, far procedure. Receives one
collection element--a GraphObject, and
calls that elements Draw method.
}
procedure CallDraw(P: PGraphObject); far;
begin
P^.Draw; { Call Draw method }
end;
begin { DrawAll }
C^.ForEach(@CallDraw); { Draw each object }
end;
{ Instantiate and draw a collection of objects }
procedure MakeCollection(var List: PCollection);
var
I: Integer;
P: PGraphObject;
begin
{ Initialize collection to hold 10 elements first, then grow by 5's }
List := New(PCollection, Init(10, 5));
for I := 1 to 12 do
begin
case I mod 3 of { Create it }
0: P := New(PGraphPoint, Init);
1: P := New(PGraphCircle, Init);
2: P := New(PGraphRect, Init);
end;
writeln;
List^.Insert(P);